﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.ComponentModel;
using System.Collections;
using StudyEnglish.Model;
using System.Collections.ObjectModel;

namespace StudyEnglish
{
    public partial class VolList : PhoneApplicationPage
    {
        int index;
        ApplicationBarIconButton select;
        ApplicationBarIconButton delete;
        ApplicationBarIconButton add;
        ApplicationBarIconButton search;

        public VolList()
        {
            InitializeComponent();
            select = new ApplicationBarIconButton();
            select.IconUri = new Uri("/Toolkit.Content/ApplicationBar.Select.png", UriKind.RelativeOrAbsolute);
            select.Text = "select";
            select.Click += select_Click;

            delete = new ApplicationBarIconButton();
            delete.IconUri = new Uri("/Toolkit.Content/ApplicationBar.Delete.png", UriKind.RelativeOrAbsolute);
            delete.Text = "delete";
            delete.Click += delete_Click;

            add = new ApplicationBarIconButton();
            add.IconUri = new Uri("/Assets/AppBar/appbar.add.rest.png", UriKind.RelativeOrAbsolute);
            add.Text = "add";
            add.Click += btnAdd_Click;

            search = new ApplicationBarIconButton();
            search.IconUri = new Uri("/Assets/AppBar/appbar.feature.search.rest.png", UriKind.RelativeOrAbsolute);
            search.Text = "search";
            search.Click += btnSearch_Click;
        }
        protected override void OnBackKeyPress(CancelEventArgs e)
        {
            base.OnBackKeyPress(e);
            if (allToDoItemsListBox.IsSelectionEnabled)
            {
                allToDoItemsListBox.IsSelectionEnabled = false;
                e.Cancel = true;
            }
        }
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            string selectedIndex;
            if (NavigationContext.QueryString.TryGetValue("index", out selectedIndex))
            {
               index = int.Parse(selectedIndex);
               App.VolVM.GetLstVolForDay(index + 1,App.userID);
               //this.DataContext = App.VolVM.LstVolForDay;
               allToDoItemsListBox.ItemsSource = App.VolVM.LstVolForDay;
               lbl_title.Text = "Ngày " + (index + 1);
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/VolAdd.xaml?index=" + (index+1).ToString(), UriKind.Relative));
        }

        private void select_Click(object sender, EventArgs e)
        {
            allToDoItemsListBox.IsSelectionEnabled = true;
        }

        private void delete_Click(object sender, EventArgs e)
        {
            for(int i=0;i<allToDoItemsListBox.SelectedItems.Count ;i++)
            {
                AppVocabulary item = (AppVocabulary)allToDoItemsListBox.SelectedItems[i];
                App.VolVM.DelVol(item);
            }
            if (allToDoItemsListBox.IsSelectionEnabled)
            {
                allToDoItemsListBox.IsSelectionEnabled = false;
                App.VolVM.GetLstVolForDay(index + 1, App.userID);
                allToDoItemsListBox.ItemsSource = App.VolVM.LstVolForDay;
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/VolSearch.xaml", UriKind.Relative));
        }

        private void allToDoItemsListBox_IsSelectionEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
        {

            while (ApplicationBar.Buttons.Count > 0)
            {
                ApplicationBar.Buttons.RemoveAt(0);
            }

            if ((bool)e.NewValue)
            {
                ApplicationBar.Buttons.Add(delete);
                ApplicationBarIconButton i = (ApplicationBarIconButton)ApplicationBar.Buttons[0];
                i.IsEnabled = false;


            }
            else
            {
                ApplicationBar.Buttons.Add(add);
                ApplicationBar.Buttons.Add(select);
                ApplicationBar.Buttons.Add(search);
            }
        }

        private void Grid_Tap_1(object sender, System.Windows.Input.GestureEventArgs e)
        {
            var element = sender as FrameworkElement;
            AppVocabulary item ;
            if (element != null)
            {
                item = element.DataContext as AppVocabulary;
                if (allToDoItemsListBox.IsSelectionEnabled)
                {
                    MultiselectItem container = allToDoItemsListBox.ItemContainerGenerator.ContainerFromItem(item) as MultiselectItem;
                    if (container != null)
                    {
                        container.IsSelected = !container.IsSelected;
                    }
                }
                else
                {
                    NavigationService.Navigate(new Uri("/VolDetail.xaml?id=" + item.ID, UriKind.Relative));
                }
            }
            
        }

      
        private void edit_Click(object sender, RoutedEventArgs e)
        {
            var menuitem = sender as MenuItem;
            var id = "";
            if (menuitem != null)
            {
                id = menuitem.Tag.ToString();
                NavigationService.Navigate(new Uri("/VolAdd.xaml?id=" + id + "&index=" + (index + 1).ToString(), UriKind.Relative));
            }
            //string id = (string) ((MenuItem)sender).Tag;
            
        }

      

        private void detail_Click(object sender, RoutedEventArgs e)
        {
            var menuitem = sender as MenuItem;
            var id = "";
            if (menuitem != null)
            {
                id = menuitem.Tag.ToString();
                NavigationService.Navigate(new Uri("/VolDetail.xaml?id=" + id , UriKind.Relative));
            }
        }

        private void allToDoItemsListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            MultiselectList target = (MultiselectList)sender;
            ApplicationBarIconButton i = (ApplicationBarIconButton)ApplicationBar.Buttons[0];

            if (target.IsSelectionEnabled)
            {

                if (target.SelectedItems.Count > 0)
                {
                    i.IsEnabled = true;
                }
                else
                {
                    i.IsEnabled = false;
                }
            }
            else
            {
                i.IsEnabled = true;
            }
        }

        private void del_Click(object sender, RoutedEventArgs e)
        {
            App.VolVM.GetVolByID((int)((MenuItem)sender).Tag);
            AppVocabulary item = App.VolVM.VolByID;
            App.VolVM.DelVol(item);
            App.VolVM.GetLstVolForDay(index + 1, App.userID);
            allToDoItemsListBox.ItemsSource = App.VolVM.LstVolForDay;
        }

        private void logout_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/UserLogin.xaml", UriKind.Relative));
        }
    }
}